Background

Recreating Case Study 10 using leaflet

You have been asked to support a story for the local paper (that has a web presence) that looks back on the housing collapse and the early effects of residential construction. You have data on residential building permits from 1980 through 2010 for each county in the United States. Your colleague that is writing the article would like a few maps and graphics that highlight the single family building permit patterns across your state as well as the patterns in the US.

Remember the big story is the collapse of new building permits at the initial stages of the mortgage crisis. Make sure your graphics highlight the collapse in a clear and honest manner.

Graphics

states <- us_states(resolution = "high")
counties <- us_counties()
permitdata <-
  full_join(permits, counties, by = c("countyname" = "countyfp"))
singlefamily <- permitdata %>%
  filter(variable == "Single Family")

singlefamily <- permitdata %>%
  filter(variable == "Single Family")

greenLeafIcon <- makeIcon(
  iconUrl = "http://leafletjs.com/examples/custom-icons/leaf-green.png",
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

leaflet(data = states) %>% 
  addTiles() %>%
  addPolygons(fillColor = topo.colors(10, alpha = NULL), 
            stroke = FALSE) #%>%
  #addMarkers(data = permitdata, icon = greenLeafIcon)
leaflet(data = permitdata) %>% 
  addTiles() %>%
  addPolygons(fillColor = topo.colors(10, alpha = NULL), 
            stroke = FALSE) %>%
  addMarkers(data = singlefamily, icon = greenLeafIcon)


states <- us_states(resolution = "high")
counties <- us_counties()
#counties2 <- 
  #st_join(states, counties, join = st_intersects, left = TRUE)

leaflet(data = states) %>% 
  addTiles() %>%
  addPolygons(fillColor = topo.colors(10, alpha = NULL), 
            stroke = FALSE) %>%
  addMarkers(data = counties)

mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
  addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
counties <- us_counties()
states <-  us_states()

permit <- 
  permits %>% 
  filter(variable == "Single Family", year == 2006 |year == 2007 | year == 2008 | year == 2009 | year == 2010, StateAbbr != "AK", StateAbbr != "HI") %>%
  mutate(statefp = str_pad( state, width = 2, side = "left", pad = 0 ),
         countyfp = str_pad( county, width = 3, pad = 0 ))
#Because I got really stuck and worked for hours on the above code I looked at code from Makayla Torsak. 
single_fam <-
  permits %>%
  filter( variable == "Single Family") %>%
  mutate(statefp = str_pad(state, width = 2, side = "left", pad = 0),
         countyfp = str_pad(county, width = 3, pad = 0))

counties <- us_counties()

all_counties <- counties %>%
  inner_join(single_fam) %>%
  filter(!state %in% c("AK", "HI", "PR"))

bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("RdYlBu", domain = all_counties$value, bins = bins)

usa <- all_counties %>%
  group_by(state_name, countyname) %>%
  leaflet() %>%
  addTiles() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addProviderTiles(providers$Esri.WorldTerrain) %>%
  addProviderTiles(providers$Stamen.TonerLines, options = providerTileOptions(opacity = 0.35)) %>%
  setView(-96, 37.8, 4) %>%
 addPolygons(fillColor = ~pal(value), weight = 2, opacity = 1, color = "white", dashArray = "3", fillOpacity = 0.7,highlight = highlightOptions(weight = 5, color = "black", dashArray = "", fillOpacity = 0.7, bringToFront = TRUE)) %>%
  addLegend(pal = pal, values = ~value, opacity = 0.7, title = NULL, position = "bottomright")
usa